var cm_query_democracygoal = new Ext.grid.ColumnModel([
	new Ext.grid.CheckboxSelectionModel({singleSelect : false}),
	new Ext.grid.RowNumberer({header : '序号', width : 40, dataIndex: 'id'}),
	{id: 'content',header: "指标内容", width: 300, sortable: true, dataIndex: 'content'},
	{id: 'spec',header: "指标说明", width: 300, sortable: true, dataIndex: 'spec'},
    {id: 'point',header: "指标分值", width: 80, sortable: true, dataIndex: 'point'},
    {id: 'ordering',header: "排序", width: 80, hidden: true, dataIndex: 'ordering'},
    {id: 'year',header: "考核年度", width: 120, dataIndex: 'year'}
]);
var ds_query_democracygoal = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : '/supervise/biz/democracygoal/findDemocracyGoalByPage.action'
	}), 
	reader : new Ext.data.JsonReader({
		root : 'root',
		totalProperty : 'totalProperty'
	}, [{
		name : 'settingId',
		type : 'int'
	}, {
		name : 'id',
		type : 'int'
	}, {
		name : 'content',
		type : 'string'
	},{
		name : 'spec',
		type : 'string'
	},{
		name : 'point',
		type : 'string'
	},{
		name : 'ordering',
		type : 'string'
	},{
		name : 'year',
		mapping : 'year',
		type : 'int'
	}])
});

var cm_democracygoalsetting = new Ext.grid.ColumnModel([
    new Ext.grid.CheckboxSelectionModel({singleSelect : true}),
	new Ext.grid.RowNumberer({header : '序号', width : 40, dataIndex: 'id'}),
	{id: 'settingId',header: "指标设置ID", width: 40, hidden: true, dataIndex: 'settingId'},
	{id: 'content',header: "指标内容", width: 300, sortable: true, dataIndex: 'content'},
	{id: 'spec',header: "指标说明", width: 300, sortable: true, dataIndex: 'spec'},
    {id: 'point',header: "指标分值", width: 80, sortable: true, dataIndex: 'point'},
    {id: 'ordering',header: "排序", width: 80, hidden: true, dataIndex: 'ordering'},
    {id: 'year',header: "考核年度", width: 120, dataIndex: 'year'}
]);
var btn_add_democracygoalsetting = new Ext.Button({
			text : '增加',
			iconCls : 'icon-add',
			handler : function() {
				var frm = Ext.getCmp('form_query_democracygoalsetting').getForm();
				var year = frm.findField('year').getValue();
				var orgType = frm.findField('orgType').getValue();
				if (Ext.isEmpty(year) || Ext.isEmpty(orgType)) {
					Ext.MessageBox.alert("提示", "请选择输入所有查询条件！");
					return;
				}
				window_query_democracygoal.show();
				window_query_democracygoal.center();
			}
		});
 
var btn_del_democracygoalsetting = new Ext.Button({
			text : '删除',
			iconCls : 'icon-del',
			handler : function() {
				var record = grid_democracygoalsetting.getSelectionModel().getSelected();
				if (record) {
					Ext.Msg.confirm('确认删除', '你确定删除该条记录?', function(btn) {
								if (btn == 'yes') {
									Ext.Ajax.request({
												url : '/supervise/biz/democracygoal/deleteSetting.action',
												params : {
													id : record.data.settingId
												},
												success : function() {
													grid_democracygoalsetting.getStore().remove(record);
													grid_democracygoalsetting.getView().refresh();
												},
												failure : function() {
													Ext.Msg.show({
																title : '错误提示',
																msg : '删除时发生错误!',
																buttons : Ext.Msg.OK,
																icon : Ext.Msg.ERROR
															});
												}
											});
								}
							});
				}
			}
		});
		
var btn_add_democracygoal_setting = new Ext.Button({
	text : '加入',
	iconCls : 'icon-add',
	handler : function() {
		if(!grid_query_democracygoal.checkMany()){
			return;
		} else {
			Ext.Msg.confirm('确认提示', '你确定要加入这些社会评价指标?', function(btn) {
				if (btn == 'yes') {
				　 	var frm = Ext.getCmp('form_query_democracygoalsetting').getForm();
					var year = frm.findField('year').getValue();
					var orgType = frm.findField('orgType').getValue();
					if (Ext.isEmpty(year) || Ext.isEmpty(orgType)) {
						Ext.MessageBox.alert("提示", "请选择输入所有查询条件！");
						return;
					}
					var records = grid_query_democracygoal.getSelectionModel().getSelections();
					var ids = [];
					for(var i = 0, count = records.length; i < count; i++){
						ids.push(records[i].get('id'));
					}
				//		提交保存数据
					Ext.getBody().mask("数据正在提交，请稍等...","x-mask-loading"); 
					Ext.Ajax.request({
						url : '/supervise/biz/democracygoal/saveSetting.action',
						success : function(response, options) {
							Ext.getBody().unmask(); 
							var store = grid_democracygoalsetting.getStore();
							var toRecord = null;
							for(var i = 0, count = records.length; i < count; i++){
								toRecord = new DemocracyGoal({
									id : records[i].get('id'),
									content : records[i].get('content'),
									point : records[i].get('point'),
									spec : records[i].get('spec'),
									ordering : records[i].get('ordering'),
									year : records[i].get('year')
								});
								store.insert(0, [toRecord]);
							}
							grid_democracygoalsetting.getView().refresh();
							window_query_democracygoal.hide();
						},
						failure : function() {
							Ext.getBody().unmask();
							Ext.Msg.alert('提示', '数据保存失败！');
						},
						params : {
							goalIds : ids.join(','),
							orgType : orgType
						}
					});
				}
			});
		}
	}
});
var ds_democracygoalsetting = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : '/supervise/biz/democracygoal/findSettingByPage.action'
	}),
	reader : new Ext.data.JsonReader({
		root : 'root',
		successProperty :'success'
	}, [{
		name : 'settingId',
		type : 'int'
	}, {
		name : 'id',
		type : 'int'
	}, {
		name : 'content',
		type : 'string'
	},{
		name : 'spec',
		type : 'string'
	},{
		name : 'point',
		type : 'string'
	},{
		name : 'ordering',
		type : 'string'
	},{
		name : 'year',
		mapping : 'year',
		type : 'int'
	}])
});

var grid_query_democracygoal = new Ext.grid.GridPanel({
		iconCls : 'icon-grid',
		loadMask : {
			msg : '数据加载中...'
		},
		height : 600,
		viewConfig : {
			forceFit : true
		},
		region : 'center',
		cm : cm_query_democracygoal,
		sm : new Ext.grid.CheckboxSelectionModel({
					singleSelect : false
				}),
		store : ds_query_democracygoal,
		tbar : [btn_add_democracygoal_setting],
		bbar : new Ext.PagingToolbar({
					pageSize : 20,
					store : ds_query_democracygoal,
					displayInfo : true,
					displayMsg : '第 {0} - {1} 条  共 {2} 条',
					refreshText : "刷新",
					emptyMsg : "没有记录"
				}),
		// 检测至少选择一个
		checkOne : function() {
			var selections = this.getSelectionModel().getSelections();
			if (selections.length == 0) {
				Ext.MessageBox.alert("提示", "请选择一条的记录！");
				return false;
			} else if (selections.length != 1) {
				Ext.MessageBox.alert("提示", "不能选择多行！");
				return false;
			}
			return true;
		},

		// 检测必须选择一个
		checkMany : function() {
			var selections = this.getSelectionModel().getSelections();
			if (selections.length == 0) {
				Ext.MessageBox.alert("提示", "请至少选择一条的记录！");
				return false;
			}
			return true;
		},
		listeners : {
			'render' : function(e) {
				var frm = Ext.getCmp('form_query_democracygoalsetting').getForm();
				var year = frm.findField('year').getValue();
				ds_query_democracygoal.baseParams = {"year" : year};
				ds_query_democracygoal.load({
							params : {
								start : 0,
								limit : 20
							}
						});
			}
		}

	});
	
var grid_democracygoalsetting = new Ext.grid.GridPanel({
			loadMask : {
				msg : '数据加载中...'
			},
			viewConfig : {
				forceFit : true
			},
			region : 'center',
			cm : cm_democracygoalsetting,
			sm : new Ext.grid.CheckboxSelectionModel({
						singleSelect : true
					}),
			store : ds_democracygoalsetting,
			autoExpandColumn : 'checkResult',
			// plugins: btn_check_col,
			tbar : [btn_add_democracygoalsetting, '-', btn_del_democracygoalsetting],
			bbar : new Ext.PagingToolbar({
						pageSize : 20,
						store : ds_democracygoalsetting,
						displayInfo : false,
						displayMsg : '第 {0} - {1} 条  共 {2} 条',
						refreshText : "刷新",
						emptyMsg : "没有记录"
					}),
			// 检测至少选择一个
			checkOne : function() {
				var selections = this.getSelectionModel().getSelections();
				if (selections.length == 0) {
					Ext.MessageBox.alert("提示", "请选择一条的记录！");
					return false;
				} else if (selections.length != 1) {
					Ext.MessageBox.alert("提示", "不能选择多行！");
					return false;
				}
				return true;
			},

			// 检测必须选择一个
			checkMany : function() {
				var selections = this.getSelectionModel().getSelections();
				if (selections.length == 0) {
					Ext.MessageBox.alert("提示", "请至少选择一条的记录！");
					return false;
				}
				return true;
			},
			listeners : {
				'render' : function(e) {
					// ds_democracygoalsetting.load();
				}
			}

		});

var qpanel_democracygoalsetting = new Ext.Panel({
			height : 100,
			region : "north",
			baseCls : "x-plain",
			style : "padding:5px",
			layout : "column",
			items : [{
						columnWidth : 0.6,
						layout : "form",
						frame : true,
						baseCls : 'x-plain',
						items : [new Ext.FormPanel({
									id : 'form_query_democracygoalsetting',
									labelAlign : 'right',
									border : false,
									baseCls : 'x-plain',
									bodyStyle : 'padding:5px 5px 0',
									labelWidth : 120,
									defaults : {
										anchor : '80%',
										width : 300,
										msgTarget : 'side' // 验证信息显示右边
									},
									defaultType : 'textfield',
									items : [{
												xtype : 'combo',
												fieldLabel : '<span style="color: red">*&nbsp;</span>考核年度',
												hiddenName : 'year',
												valueField : 'value',
												displayField : 'text',
												value : check_year,
												mode : 'local',
												store : ds_year_select,
												value : select_year,
												selectOnFocus : true,
												editable : false,
												allowBlank : false,
												typeAhead : true,
												readOnly : true,
												triggerAction : 'all',
												loadingText : '加载中...',
												emptyText : '请选择考核年度'
											},  new Ext.form.ComboBox({
														fieldLabel : '<span style="color: red">*&nbsp;</span>单位类型', // UI标签名称
														hiddenName : 'orgType',// 作为form提交时传送的参数
														allowBlank : false, // 是否允许为空
														mode : 'local', // 数据模式,local为本地模式
														readOnly : true, // 是否只读
														triggerAction : 'all', // 显示所有下列数.必须指定为'all'
														anchor : '90%',
														emptyText : '请单位类型',
														store : new Ext.data.SimpleStore({ // 填充的数据
															fields : ["retrunValue", "displayText"],
															data : [['unit', '垂直管理单位'], ['dept', '机关'], ['town', '乡镇'],
																	['project', '工业园区管委会和工程建设指挥部']]
														}),
														valueField : 'retrunValue', // 传送的值
														displayField : 'displayText',
														listeners : {
															'select' : function(combo, record, index) {
																var orgType = combo.getValue();
//																try {
//																	var queryOrg = Ext.getCmp('query_setting_orgId');
//																	queryOrg.clearValue();
//																	queryOrg.store.reload({
//																				params : {
//																					orgType : orgType
//																				}
//																			});
//																} catch (ex) {
//																	Ext.MessageBox.alert(ex);
//																}
															}
														}
													})],
									buttonAlign : 'center',
									minButtonWidth : 60,
									buttons : [{
												text : '查询',
												handler : function(btn) {
													var frm = btn.ownerCt.ownerCt.getForm();
													var year = frm.findField('year').getValue();
													var orgType = frm.findField('orgType').getValue();
													if (Ext.isEmpty(year) || Ext.isEmpty(orgType)) {
														Ext.MessageBox.alert("提示", "请选择输入所有查询条件！");
														return;
													}
													ds_democracygoalsetting.load({
																params : {
																	start : 0,
																	limit : 20,
																	year : year,
																	orgType : orgType
																}
															});
												}
											}]
								})]
					}]
		});

var democracygoalsetting_panel = new Ext.Panel({
			frame : false,
			region : 'center',
			labelWidth : 65,
			title : '社会评价指标设置',
			labelPad : 45,
			buttonAlign : 'center',
			layout : 'border',
			items : [qpanel_democracygoalsetting, grid_democracygoalsetting]
		});
// 
var p_democracygoalsetting = {
	id : 'democracygoalsetting-panel',
	border : false,
	layout : 'border',
	items : [democracygoalsetting_panel]
};

//var querypanel_democracygoal = new Ext.Panel({
//	height : 150,
//	region : "north",
//	baseCls : "x-plain",
//	style : "padding:5px",
//	layout : "column",
//	items : [{
//				columnWidth : 0.6,
//				layout : "form",
//				frame : true,
//				baseCls : 'x-plain',
//				items : [new Ext.FormPanel({
//							id : 'form_query_goal',
//							labelAlign : 'right',
//							border : false,
//							baseCls : 'x-plain',
//							bodyStyle : 'padding:5px 5px 0',
//							defaults : {
//								anchor : '90%',
//								width : 240,
//								msgTarget : 'side' // 验证信息显示右边
//							},
//							defaultType : 'textfield',
//							items : [{
//										xtype : 'combo',
//										fieldLabel : '<span style="color: red">*&nbsp;</span>考核年度',
//										hiddenName : 'year',
//										valueField : 'value',
//										displayField : 'text',
//										value : check_year,
//										mode : 'remote',
//										store : ds_year_select,
//										value : select_year,
//										selectOnFocus : true,
//										editable : false,
//										allowBlank : false,
//										typeAhead : true,
//										readOnly : true,
//										triggerAction : 'all',
//										loadingText : '加载中...',
//										emptyText : '请选择考核年度',
//										listeners : {
//											'select' : function(combo, record, index) {
//												select_year = combo.getValue();
////												var root = catTree.getRootNode();
////												root.reload();
//											}
//										}
//									}, {
//										xtype : 'hidden',
//										name : 'categoryId'
//									},{
//										fieldLabel : '考核内容',
//										name : 'content'
//									}, {
//										xtype : 'combo',
//										fieldLabel : '考核责任单位',
//										hiddenName : 'checkOrgId',
//										valueField : 'id',
//										displayField : 'name',
//										mode : 'remote',
//										store : ds_org_select,
//										// anchor : '100%',
//										selectOnFocus : true,
//										editable : false,
//										allowBlank : true,
//										triggerAction : 'all',
//										loadingText : '加载中...',
//										emptyText : '请选择部门'
//									}],
//							buttonAlign : 'center',
//							minButtonWidth : 60,
//							buttons : [{
//										text : '查询',
//										handler : function(btn) {
//											var items = btn.ownerCt.ownerCt.items;
//											ds_democracygoal.load({
//														params : {
//															start : 0,
//															limit : 20,
//															year : items.get(0).getValue(),
//															categoryId : items.get(1).getValue(),
//															content : items.get(3).getValue(),
//															checkOrgId : items.get(4).getValue()
//														}
//													});
//										}
//									}, {
//										text : '重置',
//										handler : function() {
//											var frm = Ext.getCmp('form_query_goal').getForm();
//											frm.reset();
//										}
//									}]
//						})]
//			}]
//});
//
//
var window_query_democracygoal = new Ext.Window({
	title : '增加民主评价指标',
	width : 720,
	height : 540,
	resizable : false,
	autoHeight : false,
	modal : true,
	closeAction : 'hide',
	layout : 'form',
	listeners : {
		'hide' : function() {
			var grid = window_query_democracygoal.findByType('grid')[0];
			if(grid) { 
				grid.getSelectionModel().clearSelections();
			}
		}
	},
	items: [grid_query_democracygoal]
});
